#!/bin/bash
set -euxo pipefail
# 三种变量赋值的方式
# a=2;b=3;c=4
# let a=2 b=3 c=4
# ((a=2,b=3,c=4)) 推荐使用

# 条件判断方式
# test [option] expr
# [ expr ]
# [[ expr ]] 推荐使用

# 变量赋值的两种方式，方式一效率更高
# printf -v name "%s" "hello, lgf"; echo "$name"
# name=$(printf "%s" "hello, lgf"); echo "$name"

# echo ${varName? Error: The varName is not defined}

# declare -A John=( ["age"]="23" ["weight"]="150" )
# declare -A Jackie=( ["age"]="21" ["weight"]="140" )
# declare -a dict=("John" "Jackie")

# for member in "${dict[@]}"; do
#     echo "$member :"
#     declare -n p="$member"  # now p is a reference to a variable "$member"
#     for attr in "${!p[@]}"; do
#         echo "    $attr : ${p[$attr]}"
#     done
# done

# 1.声明字典
# declare -A dic
# 2. 初始化字典
# dic=(
# ["key1"]="value1"
# ["key2"]="value2"
# ["key3"]="value3"
# )
# 3. 获取字典的值
# value=${dic["key3"]} // 获取某个键的值
# values=${dic[*]} // 获取字典中所有的值的列表
# keys=${!dic[*]} // 获取字典中所有的键的列表
# ${dic[*]} 也可用${dic[@]}替换

# 4. 修改字典的值
# dic["key1"]="newValue"
# 5. 检测键是否存在
# if [ -v dic["key1"] ]
# then
#   echo "key1 exists in dic"
# fi
# 6. 删除键值对
# unset dic["key1"]
# 7. 遍历字典

# D:\dev-env\msys64\usr\bin\bash.exe .\shell\demo.sh


# for key in "${!dic[*]}"
# do
#   echo "$key - ${dic[*]}"
# done


# declare -A dic
# dic=(
# ["key1"]="value1"
# ["key2"]="value2"
# ["key3"]="value3"
# )

# echo ${dic[key1]}

# for key in "${!dic[*]}"
# do
#   echo "$key - ${dic[*]}"
# done


if [ "$(whoami)" = "root" ]; then
  sudo_flag=""
else
  sudo_flag="sudo"
fi

lsb_dist="$(. /etc/os-release && echo "$ID")"
sub_lsb_dist="$(. /etc/os-release && echo "$VERSION_ID")"
common_pkgs='git openssh-server vim emacs curl wget bash zsh csh ksh tmux gcc clang gdb cmake make automake python3 python3-pip ruby perl'

declare -A dist_common_pkgs_dic=(
    ["debian"]=""
    ["ubuntu"]=""
    ["kali"]=""
    ["rhel"]=""
    ["centos"]=""
    ["fedora"]=""
    ["alma"]=""
    ["rocky"]=""
    ["ol"]=""
    ["arch"]=""
    ["manjaro"]=""
    ["opensuse"]=""
    ["alpine"]=""
    ["nixos"]=""
    ["gentoo"]=""
    ["freebsd"]=""
    ["drawin"]=""
    ["msys2"]=""
)

declare -A special_pkgs_dic=(
    ["kali"]=""
    ["arch"]=""
    ["manjaro"]=""
    ["nixos"]=""
    ["gentoo"]=""
    ["debian8"]=""
    ["debian9"]=""
    ["debian10"]=""
    ["debian11"]="fish"
    ["ubuntu-14.04"]=""
    ["ubuntu-15.04"]=""
    ["ubuntu-16.04"]=""
    ["ubuntu-17.04"]=""
    ["ubuntu-18.04"]="fish"
    ["ubuntu-19.04"]="fish"
    ["ubuntu-20.04"]="fish"
    ["ubuntu-21.04"]="fish"
    ["ubuntu-22.04"]="fish"
    ["ubuntu-23.04"]="fish"
    ["ubuntu-14.10"]=""
    ["ubuntu-15.10"]=""
    ["ubuntu-16.10"]=""
    ["ubuntu-17.10"]=""
    ["ubuntu-18.10"]="fish"
    ["ubuntu-19.10"]="fish"
    ["ubuntu-20.10"]="fish"
    ["ubuntu-21.10"]="fish"
    ["ubuntu-22.10"]="fish"
    ["fedora-28"]=""
    ["fedora-29"]=""
    ["fedora-30"]=""
    ["fedora-31"]=""
    ["fedora-32"]=""
    ["fedora-33"]=""
    ["fedora-34"]=""
    ["fedora-35"]=""
    ["fedora-36"]=""
    ["fedora-37"]=""
    ["fedora-38"]=""
    ["rhel6"]=""
    ["rhel6"]=""
    ["rhel8"]=""
    ["rhel9"]=""
    ["centos6"]=""
    ["centos7"]=""
    ["centos8"]=""
    ["centos8s"]=""
    ["centos9s"]=""
    ["alma8"]=""
    ["alma9"]=""
    ["rocky8"]=""
    ["rocky9"]=""
    ["freebsd-11"]=""
    ["freebsd-12"]=""
    ["freebsd-13"]=""
    ["msys2-"]="fish zsh \
        bash tcsh ksh"
)

declare -A pkg_mgt=(
    ["debian"]="$sudo_flag cp /etc/apt/sources.list /etc/apt/sources.list.bak && \
        $sudo_flag sed -i 's/(deb|security).debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list && \
        $sudo_flag apt-get install -y "
    ["ubuntu"]="$sudo_flag cp /etc/apt/sources.list /etc/apt/sources.list.bak && \
        $sudo_flag apt-get update && \
        $sudo_flag apt-get install -y ca-certificates apt-transport-https && \
        $sudo_flag sed -i 's@http://.*(archive|security).ubuntu.com@https://mirrors.ustc.edu.cn@g' /etc/apt/sources.list && \
        $sudo_flag apt-get install -y "
    ["kali"]="$sudo_flag cp /etc/apt/sources.list /etc/apt/sources.list.bak && \
        echo 'deb https://mirrors.ustc.edu.cn/kali kali-rolling main non-free contrib' | $sudo_flag tee /etc/apt/sources.list  && \
        $sudo_flag apt-get install -y "
    ["alpine"]="$sudo_flag cp /etc/apk/repositories /etc/apk/repositories.bak && \
         $sudo_flag sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories && \
         $sudo_flag apk update && $sudo_flag apk add --no-cache "
    ["rhel"]="$sudo_flag yum update -y && $sudo_flag yum install -y "
    ["centos"]="$sudo_flag yum update -y && $sudo_flag yum install -y "
    ["fedora"]="$sudo_flag yum update -y && $sudo_flag yum install -y "
    ["alma"]="$sudo_flag yum update -y && $sudo_flag yum install -y "
    ["rocky"]="$sudo_flag yum update -y && $sudo_flag yum install -y "
    ["ol"]="$sudo_flag yum update -y && $sudo_flag yum install -y "
    ["arch"]=""
    ["manjaro"]=""
    ["opensuse"]=""
    ["nixos"]=""
    ["freebsd"]=""
    ["darwin"]="$sudo_flag brew install -y "
    ["msys2"]="$sudo_flag pacman -Syy --noprogressbar --noconfirm --needed "
)

# echo ${special_pkgs_dic[$lsb_dist-$sub_lsb_dist]}
# echo ${pkg_mgt[$lsb_dist]}
# echo ${pkg_mgt[$lsb_dist]} ${common_pkgs} ${special_pkgs_dic[$lsb_dist-$sub_lsb_dist]}
# ${pkg_mgt[$lsb_dist]} ${common_pkgs} ${special_pkgs_dic[$lsb_dist-$sub_lsb_dist]}



# aa=60
# for ((i=1; i <= $aa; i++))
# do
#   printf '%0.1s' "-"
# done

# to=20
# for _ in $(eval "echo {0..$to}"); do printf '%0.1s' "-"; done

# pad=$(printf '%0.1s' "-"{1..60})
# padlength=40
# string2='bbbbbbb'
# for string1 in a aa aaaa aaaaaaaa
#     do
#         printf '%s' "$string1"
#         printf '%*.*s' 0 $((padlength - ${#string1} - ${#string2} )) "$pad"
#         printf '%s\n' "$string2"
#         string2=${string2:1}
#     done

# printf -- "-%.0b" {1..80}
# process_status(){
#     # line=$(printf '%0.1s' "-"{1..60})
#     line=$(for _ in $(eval "echo {0..$1}"); do printf '%0.1s' "-"; done)
#     PROC_NAME=$2
#     printf "%s %s [$3]\n" "$PROC_NAME" "${line:${#PROC_NAME}}"
# }

# printf '\033[1;32m[success]  %b\033[0m\n' "$1";
process_status(){
    padlength=${1:-"60"}
    process_name=${2:-"install program xxx"}
    process_status="${3:-"start"}"
    pad=$(for _ in $(eval "echo {0..$padlength}"); do printf '%0.1s' "-"; done)
    if [ "$process_status" == "done" ]; then
        printf '\033[1;32m%s' "$process_name"
    elif [ "$process_status" == "start" ]; then
        printf '\033[1;33m%s' "$process_name"
    fi
    printf '%*.*s' 0 $((padlength - ${#process_name} - ${#process_status} )) "$pad"
    printf '%b\033[0m\n' "[$process_status]"
}
process_status "100" "install oh-my-bash" "start"
echo "install oh-my-bash now!!!"
process_status "100" "install oh-my-bash" "done"
echo lgf
process_status



# if [[ $(echo $lsb_dist | grep -E "ubuntu|kali") -eq 0 ]]; then
#     echo "yes"
# else
#     echo "no"
# fi





# https://www.bbsmax.com/A/RnJWpvAO5q/
# for STYLE in 0 1 2 3 4 5 6 7; do
#   for FG in 30 31 32 33 34 35 36 37; do
#     for BG in 40 41 42 43 44 45 46 47; do
#       CTRL="\033[${STYLE};${FG};${BG}m"
#       echo -en "${CTRL}"
#       echo -n "${STYLE};${FG};${BG}"
#       echo -en "\033[0m"
#     done
#     echo
#   done
#   echo
# done
# # Reset
# echo -e "\033[0m"
